package com.ccb.jyzx.expand.tiantouonline.service.impl;

import com.ccb.jyzx.expand.tiantouonline.bean.*;
import com.ccb.jyzx.expand.tiantouonline.bean.vo.ClassifyVo;
import com.ccb.jyzx.expand.tiantouonline.bean.vo.DictVo;
import com.ccb.jyzx.expand.tiantouonline.bean.vo.FileInfoVo;
import com.ccb.jyzx.expand.tiantouonline.dao.*;
import com.ccb.jyzx.expand.tiantouonline.service.BatchPushService;
import com.ccb.jyzx.expand.tiantouonline.service.ClassifyService;
import com.ccb.jyzx.expand.tiantouonline.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @projectName: extend
 * @author: jianghaolin
 * @date: 2019/11/19
 * @version: 1.0
 */
@Service
public class ClassifyServiceImpl implements ClassifyService {

    Logger logger= LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DictDao dictDao;

    @Autowired
    private DictGroupDao dictGroupDao;

    @Autowired
    private BatchInfoDao batchInfoDao;

    @Autowired
    private FileInfoDao fileInfoDao;

    @Autowired
    private FileTagsInfoDao fileTagsInfoDao;

    @Autowired
    private BatchPushService batchPushService;

    @Override
    public ClassifyVo findCascade() {
        List<Dict> dicts = dictDao.findAll();
        ClassifyVo vo = new ClassifyVo();
        List<DictVo> childrenes = new ArrayList<>();
        //首先将dict按结构拿出来
        if(dicts != null && dicts.size() > 0){
            for(Dict dict : dicts){
                Integer level = dict.getLevel();
                if(level == 1){
                    String code = dict.getDictCode();
                    DictVo oneChildrens = new DictVo();
                    UpdateTool.copyNullProperties(dict,oneChildrens);
                    List<DictVo> childrens = recursion(dicts,code);
                    oneChildrens.setChildrens(childrens);
                    childrenes.add(oneChildrens);
                }
            }
        }
        vo.setChildrens(childrenes);
        return vo;
    }

    @Override
    public Boolean getTag(String filePath,String tagLab) {
        logger.info("生成tag文件-----filePath:" + filePath);
        boolean result = false;
        List<FileInfoVo> fileInfos = batchInfoDao.listNoPush();
        if(fileInfos != null && fileInfos.size() > 0){
            logger.info("fileInfos的长度:" + fileInfos.size());
            List<String> tagContents = getNowTagContents(fileInfos,tagLab);
            //获取本次覆盖的
            List<BatchPush> coverFiles = batchPushService.findAllCoverFiles();
            if(coverFiles != null && coverFiles.size() > 0){
                logger.info("覆盖文件的长度:" + coverFiles.size());
                for(BatchPush coverFile : coverFiles){
                    tagContents.add(coverFile.getTag());
                }
            }
            result = FileUtils.createFile(filePath,tagContents);
        }

        return result;
    }


    /**
     * 获取本次的tag内容
     * @param fileInfos
     * @return
     */
    private List<String> getNowTagContents(List<FileInfoVo> fileInfos,String tagLab){
        List<String> tagContents = new ArrayList<>();
        int i = 1;
        for(FileInfoVo fi : fileInfos){
            FileTagsInfo ti =fileTagsInfoDao.findByInfoid(fi);
            if(ti != null) {
                String id = fi.getId();
                String iStr = i + "";
                String tIstr = "0" + i;
                Classify classify = new Classify();
                String typeId = ti.getTypeId();
                if(typeId != null && typeId.length() > 0){
                    typeId = typeId.substring(1,2);
                }
                String frequentness = ti.getTypeId();
                //到时候修改该处
                String reportTime = DateUtils.getYestoday();
                String fileSeq = iStr.length() == 1 ? tIstr:iStr;
                String year = ti.getYear() == null ? DateUtils.nowYear() : ti.getYear();
                String month = ti.getMonth();
                String quarter = ti.getQuarter();
                String week = ti.getWeek();
                String oneId = ti.getOneId();
                String twoId = ti.getTwoId();
                String threeId = ti.getThreeId();
                String name = ti.getName();
                String tagId = ti.getTagId();
                classify.setFrequentness(frequentness);
                classify.setReportTime(reportTime);
                classify.setFileSeq(fileSeq);
                classify.setBatchSeq(DateUtils.getMorningOrAfternoone());
                classify.setInfoName(name);
                classify.setReportType(typeId);
                classify.setYear(year);
                classify.setQuarter(quarter);
                classify.setMonthly(month);
                classify.setWeek(week);
                classify.setTag(tagId);
                classify.setFirstColumn(oneId);
                classify.setSecondColumn(twoId);
                classify.setThirdColumn(threeId);
                String tagContext = ClassifyUtils.getTag(classify);
                logger.info("tagContext:" + tagContext);
                if("word".equals(tagLab)){
                    BatchPush batchPush = new BatchPush();
                    batchPush.setFileId(id);
                    batchPush.setFileSeq(fileSeq);
                    batchPush.setTag(tagContext);
                    batchPush.setBatchInfoId(fi.getBatchInfoId());
                    batchPush.setWordLastName(fi.getFileExten());
                    String sendTo = "/"+DateUtils.getYestoday() + "/";
                    batchPush.setSendTo(sendTo);
                    String batchPushId = IDUtils.createNumId(32);
                    batchPush.setId(batchPushId);
                    batchPush.setCreateTime(System.currentTimeMillis());
                    batchPushService.save(batchPush);
                }
                i = i + 1;
                tagContents.add(tagContext);
            }
        }
        return tagContents;
    }

    /**
     * 递归计算DictVo
     * @param dicts
     * @return
     */
    private List<DictVo> recursion(List<Dict> dicts, String code){
        List<DictVo> vos = new ArrayList<>();
        if(dicts != null && dicts.size() > 0){
            for(Dict dict : dicts){
                DictVo vo = new DictVo();
                String father = dict.getFatherCode();
                String mine = dict.getDictCode();
                if(code.equals(father)){
                    UpdateTool.copyNullProperties(dict,vo);
                    List<DictVo> children = recursion(dicts,mine);
                    vo.setChildrens(children);
                    vos.add(vo);
                }
            }
        }
        return vos;
    }
}
